package com.example.leave;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;

public class LoginPage {
    public static void main(String[] args) {
        // 创建窗口
        JFrame frame = new JFrame("用户登录页面");

        // 创建面板
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // 使用垂直布局使组件更整齐

        // 创建账号输入框
        JPanel userPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel userLabel = new JLabel("账号：");
        JTextField userText = new JTextField(20);
        userPanel.add(userLabel);
        userPanel.add(userText);
        panel.add(userPanel);

        // 创建密码输入框
        JPanel passwordPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel passwordLabel = new JLabel("密码：");
        JPasswordField passwordText = new JPasswordField(20);
        passwordPanel.add(passwordLabel);
        passwordPanel.add(passwordText);
        panel.add(passwordPanel);

        // 创建角色选择单选按钮
        JPanel rolePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JLabel roleLabel = new JLabel();
        JRadioButton studentButton = new JRadioButton("学生");
        JRadioButton teacherButton = new JRadioButton("老师");
        JRadioButton counselorButton = new JRadioButton("辅导员");

        // 创建按钮组，将单选按钮加入按钮组
        ButtonGroup roleGroup = new ButtonGroup();
        roleGroup.add(studentButton);
        roleGroup.add(teacherButton);
        roleGroup.add(counselorButton);

        rolePanel.add(roleLabel);
        rolePanel.add(studentButton);
        rolePanel.add(teacherButton);
        rolePanel.add(counselorButton);
        panel.add(rolePanel);

        // 创建登录按钮并添加事件监听器
        JButton loginButton = new JButton("登录");
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = userText.getText();
                String password = new String(passwordText.getPassword());
                String role = "None";

                // 检查哪个单选按钮被选中
                if (studentButton.isSelected()) {
                    role = "学生";
                    new Student(username); // 可添加相关逻辑 
                } else if (teacherButton.isSelected()) {
                    role = "老师";
                     new Teacher(username); // 可添加相关逻辑
                } else if (counselorButton.isSelected()) {
                    role = "辅导员";
                     new Counselor(username); // 可添加相关逻辑
                }
                // 关闭登录窗口
                frame.dispose();
            }
        });

        // 创建取消按钮并添加事件监听器
        JButton cancelButton = new JButton("取消");
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                userText.setText("");
                passwordText.setText("");
                roleGroup.clearSelection();
            }
        });

        // 创建一个面板用于放置按钮，并设置布局
        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(loginButton);
        buttonPanel.add(cancelButton);
        panel.add(buttonPanel);

        // 设置窗口属性
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 250);
        frame.add(panel);
        frame.setVisible(true);
    }
}
